package bin_tree.leetcode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author shkstart
 * @create 2022-10-18-12:37
 */
public class Num101 {
//    public boolean isSymmetric(TreeNode root) {
//        if (root == null) {
//            return true;
//        }
//        //root.left和root.right是否互为镜像
//        return isMirror(root.left, root.right);
//    }

    //    //    判断传入的两颗树是否互为镜像
//    private boolean isMirror(TreeNode left, TreeNode right) {
//        if (left == null && right == null) {
//            return true;
//        }
//        if(left == null || right == null){
//            return false;
//        }
//        if (left.val != right.val) {
//            return false;
//        }
//
//
//    }
    public boolean isSymmetric(TreeNode root) {
        if (root == null || (root.left == null && root.right == null)) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root.left);
        queue.offer(root.right);
        while (!queue.isEmpty()) {
            TreeNode left = queue.poll();
            TreeNode right = queue.poll();
            if (left == null && right == null) {
                continue;
            }
            if (left == null || right == null) {
                return false;
            }
            if (left.val != right.val) {
                return false;
            }

            queue.offer(left.left);
            queue.offer(right.right);
            queue.offer(left.right);
            queue.offer(right.left);
        }
        return true;
    }


}
